﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace QuantumQuest
{
    public partial class QQGame : UserControl
    {
        private DispatcherTimer _gameLoopTimer;
        
        private int _fps = 0;
        private DateTime _lastFps = DateTime.Now;

        private QQList _levels;


        public QQGame()
        {
            InitializeComponent();
            InitializeLevels();
            InitializeGameLoop();
            
            
           

        }

      
        private void GetFps()
        {
            _fps++;
            if ((DateTime.Now - _lastFps).Seconds >= 1)
            {
                _fps = 0;
                _lastFps = DateTime.Now;
            }
        }
        private void InitializeGameLoop()
        {
            _gameLoopTimer = new DispatcherTimer();
            _gameLoopTimer.Interval = TimeSpan.FromSeconds(1.0/(QQGlobal.FramerateSeconds));
            _gameLoopTimer.Tick += new EventHandler(_gameLoopTimer_Tick);
            _gameLoopTimer.Start();
        }

        void _gameLoopTimer_Tick(object sender, EventArgs e)
        {
            GetFps();

            for (int i = 0; i < _levels.Count; i++)
            {
                GameCanvas.Children.Add((UIElement)_levels[i]);
                while (!((QQLevel)_levels[i]).IsFinished)
                {
                    
                    ((QQLevel)_levels[i]).Step();
                }
                GameCanvas.Children.Clear();
            }

        }
        private void InitializeLevels()
        {
            _levels = new QQList(QQGlobal.NumLevels);
            QQTestLevel testLevel = new QQTestLevel();

            _levels.Add(testLevel);

           
        }

    }
}
